<?php

/**
 * This is the model class for table "individual_config".
 *
 * The followings are the available columns in table 'individual_config':
 * @property integer $id_config
 * @property integer $id_user
 * @property string $language
 * @property integer $wiki_help
 * @property integer $pagination
 * @property string $restrict_ips
 * @property integer $allow_reset_restrict_ip_byemail
 */
class IndividualConfig extends CActiveRecord
{
	const WIKI_ENABLED = 1;
	const WIKI_DISABLED = 0;
	
	const RESET_RESTRICTIP_BYEMAIL_ENABLED = 1;
	const RESET_RESTRICTIP_BYEMAIL_DISABLED = 0;
	
	const LANGUAGE_EN = 'en';
	const LANGUAGE_ESP = 'espanol';
	const LANGUAGE_LE = 'lefrancais';
	const LANGUAGE_DEU = 'deutsch';
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return IndividualConfig the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'individual_config';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_user, wiki_help, pagination', 'numerical', 'integerOnly'=>true),
			array('language, restrict_ips, allow_reset_restrict_ip_byemail', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_config, id_user, language, wiki_help, pagination, restrict_ips, allow_reset_restrict_ip_byemail', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'User' => array(self::BELONGS_TO, 'User', array('id_user' => 'id_user')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_config' => 'Id Config',
			'id_user' => 'Id User',
			'language' => 'Language',
			'wiki_help' => 'Wiki Help',
			'pagination' => 'Pagination',
			'restrict_ips' => 'Restrict Ips',
			'allow_reset_restrict_ip_byemail' => 'Allow reset IP restriction by email'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id_config',$this->id_config);
		$criteria->compare('id_user',$this->id_user);
		$criteria->compare('language',$this->language,true);
		$criteria->compare('wiki_help',$this->wiki_help);
		$criteria->compare('pagination',$this->pagination);
		$criteria->compare('restrict_ips',$this->restrict_ips,true);
		$criteria->compare('allow_reset_restrict_ip_byemail',$this->allow_reset_restrict_ip_byemail,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	const DEFAULT_PAGE_SIZE = 5;
	public function getPagination()
	{
		return empty($this->pagination)? self::DEFAULT_PAGE_SIZE : $this->pagination;
	}
	
	public function getRestrictIp()
	{
		$restrictIps = trim($this->restrict_ips);
		$restrictIps = str_replace("\r\n", "\n", $restrictIps);
		$restrictIps = str_replace("\r", "\n", $restrictIps);
		
		$restrictIps = empty($restrictIps)? array() : explode("\n", $restrictIps);
		return $restrictIps;
	}
	
	public function validateIp($ip)
	{
		$restrictIps = $this->getRestrictIp();
		if(empty($restrictIps)){
			return true;
		}
		
		return in_array($ip, $restrictIps);
	}
}